const { execSQL } = require('../../db/mysql');

//获取管理员列表方法
function getList() {
    const sql = 'select * from mng';
    return execSQL(sql);
}

//获取详情数据方法
const getDetail = id => {
    const sql = `select * from mng where mng_id='${id}'`;
    return execSQL(sql);
}

//创建新管理员
const createNewManager = (managerData = {}) => {
    const account = managerData.account; 
    const password = managerData.password;
    const avatar = managerData.avatar;

    const sql = `insert into mng (mng_account, mng_password, mng_avatar) values ('${account}', '${password}', '${avatar}')`;
    return execSQL(sql).then(insertedResult => {
        return {
            id: insertedResult.insertId
        }
    });
}

//更新管理员
const updateManager = (id, managerData = {}) => {
    const account = managerData.account;
    const password = managerData.password;
    const avatar = managerData.avatar;

    const sql = `update mng set mng_account='${account}', mng_password='${password}', mng_avatar='${avatar}' where mng_id=${id}`;
    return execSQL(sql).then(updateResult => {
        if (updateResult.affectedRows > 0) {
            return true;
        }
        return false;
    }); 
}

//删除管理员
const deleteManager = id => {
    const sql = `delete from mng where mng_id=${id}` 
    return execSQL(sql).then(deleteResult => {
        if (deleteResult.affectedRows > 0) {
            return true;
        }
        return false;
    })
}

//管理员登录
const managerLogin = (managerAccount, password) => {
    const sql = `select * from mng where mng_account='${managerAccount}'`;
    // console.log(execSQL(sql));
    return execSQL(sql);
}

//导出方法
module.exports = { 
    getList,
    getDetail,
    createNewManager,
    updateManager,
    deleteManager,
    managerLogin
}